package StudyCode.summary.aclfile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.*;
import java.util.List;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class GrantNewAccessACL {
	public static void main(String[] args) {
		Path path = Paths.get("E:/Copy", "1.txt");
		try {
			UserPrincipal user = path.getFileSystem().getUserPrincipalLookupService()
					.lookupPrincipalByName("tah1986.com");
			AclFileAttributeView view = Files.getFileAttributeView(path,
					AclFileAttributeView.class);
			AclEntry entry = AclEntry
					.newBuilder()
					.setType(AclEntryType.ALLOW)
					.setPrincipal(user)
					.setPermissions(AclEntryPermission.READ_DATA,
							AclEntryPermission.APPEND_DATA).build();

			List<AclEntry> acl = view.getAcl();
			acl.add(0, entry);
			view.setAcl(acl);
		} catch (IOException e) {
			System.err.println(e);
		}
	}
}
